package learn.advance.synchronization1;

import sun.awt.windows.WPrinterJob;

import javax.swing.*;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class RunnabelImpl implements Runnable {

    private static int ticket_ = 100;

    @Override
    public void run() {
        while (true) {
            PayTicket();
        }
    }

    public void PayTicket() {
        synchronized (this) {
            if (ticket_ > 0) {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                System.out.println(Thread.currentThread().getName());
                ticket_--;
            }
        }
    }

    public static void PayTicketStatic() {
        synchronized (RunnabelImpl.class) {
            if (ticket_ > 0) {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                System.out.println(Thread.currentThread().getName());
                ticket_--;
            }
        }
    }
}

class RunnabelImpl2 implements Runnable {
    private int ticket_ = 100;

    private Lock lock_ = new ReentrantLock();

    @Override
    public void run() {
        while (true) {
            lock_.lock();
            if (ticket_ > 0) {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                System.out.println(Thread.currentThread().getName());
                ticket_--;
            }
            lock_.unlock();
        }
    }
}

class Demo1WaitAndNotify {
    public static void Test() {

        Object obj_ = new Object();
        new Thread() {
            @Override
            public void run() {
                while (true) {
                    synchronized (obj_) {
                        System.out.println("告知老板要的包子的种类和数量");
                        try {
                            obj_.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }

            }
        }.start();

        new Thread() {
            @Override
            public void run() {
                while (true) {

                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    //保证等待和唤醒的线程只能有一个执行,需要使用同步技术
                    synchronized (obj_) {
                        System.out.println("老板5秒钟之后做好包子,告知顾客,可以吃包子了");
                        obj_.notify();
                    }
                }
            }
        }.start();
    }
}


class RunnableImp2 implements Runnable {

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
    }
}


class Demo1ThreadPool {
    public static void Test() {

        ExecutorService executor_service = Executors.newFixedThreadPool(3);

        executor_service.submit(new RunnableImp2());
        executor_service.submit(new RunnableImp2());
        executor_service.submit(new RunnableImp2());

        executor_service.shutdown(); //销毁线程池

        //executor_service.shutdown(new RunnableImp2());
    }
}




public class TestSynchronization {
    public static void main(String[] args) {
        //Demo1WaitAndNotify.Test();
        //Demo1ThreadPool.Test();

    }

}
